<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountEventLog;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountEventFunction;

class WechatOffiaccountEventLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '事件日志';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountEventLog());
        $grid->model()->orderByDesc('id');

        $grid->column('id', __('Id'))->hide();
        
        $grid->column('event_function.name', __('laravel_admin_wechat.Event id'));
        $grid->column('openid', __('laravel_admin_wechat.Openid'));
        $grid->column('event', __('laravel_admin_wechat.Event'));
        $grid->column('key', __('laravel_admin_wechat.Key'));
        $grid->column('created_at', __('laravel_admin_wechat.Created at'));

        $grid->disableFilter(false);
		$grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();
        
            // 在这里添加字段过滤器
            $filter->equal('event_function_id',__('laravel_admin_wechat.Event id'))->select(WechatOffiaccountEventFunction::select('id','name')->get()->pluck('name','id')->all());
            $filter->between('created_at',__('laravel_admin_wechat.Created at'))->datetime();
			
			
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->footer(function ($query) {

            // 查询去重后总数量
            $data = $query->select('openid')->distinct('openid')->count();
        
            return "<h4 style='padding: 10px;'>用户去重后次数 ： $data</h4>";
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountEventLog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('event_function_id', __('laravel_admin_wechat.Event id'));
        $show->field('openid', __('laravel_admin_wechat.Openid'));
        $show->field('event', __('laravel_admin_wechat.Event'));
        $show->field('key', __('laravel_admin_wechat.Key'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountEventLog());

        $form->number('event_function_id', __('laravel_admin_wechat.Event id'));
        $form->text('openid', __('laravel_admin_wechat.Openid'));
        $form->text('event', __('laravel_admin_wechat.Event'));
        $form->text('key', __('laravel_admin_wechat.Key'));

        return $form;
    }
}
